package model.jgap;
//
//import java.io.*;
//import java.util.Date;
//import java.util.List;
//
//import org.jgap.*;
//import org.jgap.data.*;
//import org.jgap.impl.*;
//import org.jgap.xml.*;
//import org.w3c.dom.*;
//
//
public class Controller {
//  
//  /**
//   * The total number of times we'll let the population evolve.
//   */
//  private static final int MAX_ALLOWED_EVOLUTIONS = 5;
//  
//  private static File logFile = new File("C:\\temp\\jgapmodellog"+System.currentTimeMillis()+".txt");
//  private static int loop = 0;
////AMJGAP: validate the gene so that parameters dont change too much one day to the next.
//  //AMJGAP: get the trade volumes out
//  //AMJGAP: for extra periods, use similar params to other days..
//  //AMJGAP: check retirement logic
//  //AMJGAP: set up nominal increases to relate to nomical GDP growth or something, then chek value investor logic.
//  
//  /**
//   * Executes the genetic algorithm to determine the most appropriate
//   * parameters to the market model to produce the given fluctuations.
//   * 
//   * @param reality - the asset price ticks.
//   * @throws Exception
//   *
//   * 
//   */
//  public static double[] matchAssetPrices(double[] reality)
//      throws Exception {
//    
//	Configuration conf = new DefaultConfiguration();
//    conf.setPreservFittestIndividual(true);
//    conf.setKeepPopulationSizeConstant(false);
//    
//    MarketModelFitnessFunction myFunc = new MarketModelFitnessFunction(reality);
//
//    //conf.setBulkFitnessFunction(new BulkFitnessOffsetRemover(myFunc));
//    conf.setFitnessFunction(myFunc);
//    
//    Gene[] sampleGenes =  new Gene[5];//AMTEMPXX:  possibly 4];
//    //Initial trader config..
//    CompositeGene initialTraders = new CompositeGene(conf);
//    sampleGenes[0] = initialTraders;
//    //AM: configure initial trader setup.
//    for(int i=0;i<5;i++){
//    	initialTraders.addGene(new TraderPositionComposite(conf,new ValueInvestorGene(conf)));
//    }
//    for(int i=0;i<15;i++){
//    	initialTraders.addGene(new TraderPositionComposite(conf,new MomentumTraderGene(conf)));
//    }
//    //AMXX: opportunists being parameterised seems like a bad idea..
////    for(int i=0;i<5;i++){
////    	initialTraders.addGene(new TraderPositionComposite(conf,new OpportunistGene(conf)));
////    }
//    
//    sampleGenes[1]= new IntegerGene(conf,0,0);//AMTEMPXX: possibly..
//    sampleGenes[2]=new DoubleGene(conf,.5,1.5);
//    //Stepped trader config...
//    CompositeGene steppedTraders =new CompositeGene(conf);
//    sampleGenes[4] = steppedTraders;
//    
//    //Interest rate composite...//AMXX: not interest rates, cashflow rates.
////    sampleGenes[2] = new CompositeGene(conf);
////    
////    //Fair value rate composite..
//    CompositeGene fairValueMovementRate = new CompositeGene(conf);
//    sampleGenes[3] = fairValueMovementRate;
//    
//    
//    for (int j = 0; j < reality.length; j++) {
//     
//    	CompositeGene dailyTradersComposite = new CompositeGene(conf);
//    	dailyTradersComposite.addGene(new TraderPositionComposite(conf,new MomentumTraderGene(conf)));
//    	dailyTradersComposite.addGene(new TraderPositionComposite(conf,new ValueInvestorGene(conf)));
//    	dailyTradersComposite.addGene(new TraderPositionComposite(conf,new OpportunistGene(conf)));
//    	
//    	steppedTraders.addGene(dailyTradersComposite);
//    	//AMXX: temp sampleGenes[2].addGene(new DoubleGene(conf,-.2,.2));
//    	//AMXX: first fair value adjustment here will be ignored right??
//    	fairValueMovementRate.addGene(new DoubleGene(conf,-.05,.05));
//    }
//
//    IChromosome sampleChromosome = new Chromosome(conf, sampleGenes);
//    conf.setSampleChromosome(sampleChromosome);
//  
//    conf.setPopulationSize(300);  
//    Genotype population = Genotype.randomInitialGenotype(conf);
//    
//    // Evolve the population. Since we don't know what the best answer
//    // is going to be, we just evolve the max number of times.
//    // ---------------------------------------------------------------
//    long startTime = System.currentTimeMillis();
//    //AMXX: could also have check for greatest correlation and break when reached.
//    int evolveCount = 0;
//    for (; evolveCount < MAX_ALLOWED_EVOLUTIONS;evolveCount++) {
//    	System.out.println("At number: "+evolveCount+" population: "+population.getPopulation().size());
////    	List fittestChromosomes = population.getFittestChromosomes(10);
////    	for(int chromCount=1;chromCount<fittestChromosomes.size();chromCount++){
////    		if(fittestChromosomes.get(chromCount).equals(fittestChromosomes.get(chromCount-1))){
////    			System.out.println("summat fu*ked.");
////    		}
////    	}
//    	population.evolve();
//    }
//    long endTime = System.currentTimeMillis();
//    System.out.println("Total evolution time: " + ( endTime - startTime)
//                       + " ms, evolutions "+evolveCount);
//    // Save progress to file. A new run of this example will then be able to
//    // resume where it stopped before!
//    // ---------------------------------------------------------------------
//
////AMXX: the jgap code for this stuff is shit.    
//    // Represent Genotype as tree with elements Chromomes and Genes.
////    DataTreeBuilder builder = DataTreeBuilder.getInstance();
////    IDataCreators doc2 = builder.representChromosomeAsDocument(population.getFittestChromosome());
////    // create XML document from generated tree
////    XMLDocumentBuilder docbuilder = new XMLDocumentBuilder();
////    Document xmlDoc = (Document) docbuilder.buildDocument(doc2);
////    XMLManager.writeFile(xmlDoc, new File("C:\\temp\\JGAPExample"+System.currentTimeMillis()+".xml"));
//    // Display the best solution we found.
//    // -----------------------------------
////    System.out.println("wassaap");
////    StringBuffer results = new StringBuffer();
////    Gene[] fitGenes = population.getFittestChromosome().getGenes();
////    for(int i=0;i<fitGenes.length;i++){
////    	Gene current = fitGenes[i];
////    	results.append("\n<gene>"+current.getPersistentRepresentation()+"</gene>");
////    	
////    }
////    System.out.println("wassaap2");
////    long currentTime = System.currentTimeMillis();
////    FileWriter writer = new FileWriter(new File("C:\\workspace\\Market\\JGAPExample"+currentTime+".xml"));
////    writer.write(results.toString());
////    writer.flush();
////    writer.close();
////    System.out.println("wassaap3");
//    IChromosome bestSolutionSoFar = population.getFittestChromosome();
//    
//    System.out.println("The best solution has a fitness value of " +
//                       bestSolutionSoFar.getFitnessValue());
////    System.out.println("It contained the following: ");
////    Gene[] genes = bestSolutionSoFar.getGenes();
////    for(int i=0;i<genes.length;i++){
////    	System.out.println(genes[i]);
////    	
////    }
//    Configuration.reset();
//    double[] resultsArray = myFunc.evaluateWithResults(bestSolutionSoFar);
//    
//    
//    
////  AMXX: set scale for correlation..
//    //AM: we work out the average year on year volatility of asset and adjust our figues for same volatility..
//    double totalAssetReturn=0;
//    for(int i=1;i<reality.length;i++){
//    	double doubleReturn = (reality[i]-reality[i-1])/reality[i-1];
//    	totalAssetReturn+=doubleReturn;//AMTEMPXX: maybe Math.abs(doubleReturn);
//    }
//    double averageAssetReturn = ((double)totalAssetReturn)/((double)reality.length-1);
//    
//    double totalModelReturn=0;
//    double[] modelReturns = new double[resultsArray.length-1];
//    for(int i=1;i<resultsArray.length;i++){
//    	double doubleReturn = (resultsArray[i]-resultsArray[i-1])/resultsArray[i-1];
//    	modelReturns[i-1]=doubleReturn;
//    	totalModelReturn+=doubleReturn;//AMTEMPXX: maybe.Math.abs(doubleReturn);
//    }
//    double averageModelReturn = ((double)totalModelReturn)/((double)resultsArray.length-1);
//    
//    double returnMultiplier = averageAssetReturn/averageModelReturn;
//    for(int i=0;i<modelReturns.length;i++){
//    	modelReturns[i]*=returnMultiplier;
//    }
//    
//    for(int i=1;i<resultsArray.length;i++){
//    	resultsArray[i]=(1+(modelReturns[i-1]*returnMultiplier))*resultsArray[i-1];
//    }
//    
////    System.out.println("Total Model Return: "+totalModelReturn+ " average model "+averageModelReturn
////    		+"\n  total asset "+totalAssetReturn+"  average asset "+averageAssetReturn+ "\n return multiplier "+returnMultiplier);
//    
//    
//    //AM: now we update the numbers so that the first one matches the initial
//    //	reality value...
//    double multiplier = reality[reality.length-1]/resultsArray[reality.length-1];//AM now maatch at the end so we can apply next directly.
//    
//    for(int i=0;i<resultsArray.length;i++){
//    	resultsArray[i]=resultsArray[i]*multiplier;
//    	
//    }
//   
//    
//    return resultsArray;
//    
//  }
//
//  /**
//   * Main method. A single command-line argument is expected, which is the
//   * amount of change to create (in other words, 75 would be equal to 75
//   * cents).
//   *
//   * @param args amount of change in cents to create
//   * @throws Exception
//   *
//   * @author Neil Rotstan
//   * @author Klaus Meffert
//   * @since 1.0
//   */
//  public static void main(String[] args) throws Exception {
//	//double[] forTest ={300,200,300,400,600,1000,999,998,950,500,300,400,500,500,500,600,500,500,400,500,500,500,550,500};	
//	
//	  double[] realHousePrices ={
//			  69680//,65790,64460,64102
//			  ,62995//,61911,61866,60254
//			  ,58193//,57053,57443,57383
//			  ,59307//,60555,65309,67838
//			  ,69717//,72016,72458,75631
//			  ,74489//,72553,71916,70178
//			  ,69178//,66955,66141,63484
//			  ,63432//,62763,63191,64266
//			  ,65776//,67096,68144,68429
//			  ,70537//,71806,72138,74287
//			  ,74799//,74482,75392,76670
//			  ,76566//,78448,80985,82811
//			  ,84483//,87438,90204,89084
//			  ,90126//,95501,104602,107965
//			  ,110473//,112284,112180,107745
//			  ,102580//,97027,92626,87496
//			  ,86438//,85978,84798,82045
//			  ,79419//,78473,77903,74487
//			  ,74911//,76375,75906,74621
//			  ,74920//,73674,74152,74238
//			  ,72127//,71586,70981,70329
//			  ,70556//,71937,73118,74253
//			  ,74668//,77153,79482,80248
//			  ,81387//,82851,84031,83589
//			  ,85421//,87698,90572,92729
//			  ,96147//,98657,98161,98368
//			  ,101315//,104456,108459,110354
//			  ,113655//,121891,130079,134855
//			  ,138726//,143343,147942,151746
//			  ,158132//,165175,169757,167028
//			  ,166943//,170110,169635,168415
//			  ,171110//,173113,175198,177081
//			  ,179248//AM: test,182667,184131	  
//			  
//	  };   
//	  
//	  double[] ftseMonthly = {
////AMTEMPXX: temp			1108.1,1136.8,1026.8,1041.3,1009.5,1105.6,1137.5,1148.4,1187.9,1230.3,1275.2,1256.7,
////			1276.7,1293.7,1315.8,1229.9,1271.7,1335.1,1292.8,1372.1,1439.6,1412.2,1431.5,1545.2,1671.3,1631.6,
////			1604.4,1652.5,1555,1655.3,1561,1639.2,1637.8,1677.6,1821.6,1986.6,1997.1,2064.4,2210,2272.6,2356.1,
////			2261.6,2372.1,1720.6,1591.5,1729.8,1797.4,1782.1,1737,1806.2,1802.5,1862.1,1861.7,1739.5,1817.4,
////			1856.1,1785.7,1783.9,2042.5,2017.9,2074.1,2109.5,2118.6,2159.3,2292.4,2392.1,2287.6,2146,2297.3,
////			2442.4,2346.5,2246.2,2224.3,2111.8,2345.3,2373.6,2329.3,2166.2,2006.3,2039.9,2159.7,2142.9,2168.5,
////			2374.9,2440.2,2487.4,2497.7,2416.7,2586.2,2653.9,2643.4,2559,2412.4,2483.1,2563.7,2552.2,2384.1,
////			2660.1,2697.7,2525.6,2390.8,2313,2558.4,2657.1,2772,2841.8,2821.9,2861.9,2871.3,2820.3,2836.1,2901.5,
////			2920,3099.5,3039,3170.7,3197,3427.2,3511.4,3322.9,3060.9,3131.9,2984.2,2914.1,3077.8,3243.6,3024.3,
////			3081.5,3079.6,3062.6,3001.8,3020,3134.2,3212.8,3352.3,3310.2,3458.7,3484.7,3510.6,3517.5,3667.5,3696,
////			3753.3,3729.5,3692.1,3816.9,3737,3710.4,3709.3,3873.7,3954.7,3982.7,4055.9,4079.9,4272.3,4293.4,4205.1,
////			4435.9,4644.7,4606,4914.6,4816.6,5241.4,4877,4871.7,5136,5589.9,5767.3,5930.9,5932.7,5870.7,5840.6,
////			5832.2,5139.5,5024.8,5454.3,5723.3,5909.4,5926,6177.5,6283.1,6593.5,6231,6372.1,6228.4,6251.1,6034.1,
////			6251.3,6591.8,6930.2,6268.5,6232.6,6540.2,6327.4,6359.3,6312.7,6365.3,6672.7,6294.2,6438.4,6142.2,
//			6222.5,6297.5,5917.9,5633.7,5966.9,5796.1,5642.5,5529.1,5345,4903.4,5039.7,5203.6,5217.4,5164.8,5101,
//			5271.8,5165.6,5085.1,4656.4,4246.2,4227.3,3721.8,4039.7,4169.4,3940.4,3567.4,3655.6,3613.3,3926,4048.1,
//			4031.2,4157,4161.1,4091.3,4287.6,4342.6,4476.9,4390.7,4492.2,4385.7,4489.7,4430.7,4464.1,4413.1,4459.3,
//			4570.8,4624.2,4703.2,4814.3,4852.3,4968.5,4894.4,4801.7,4964,5113.2,5282.3,5296.9,5477.7,5317.3,5423.2,
//			5618.8,5760.3,5791.5,5964.6,6023.1,5723.8,5833.4,5928.3,5906.1,5960.8,6129.2,6048.8,6220.8,6203.1,6171.5,
//			6308,6449.2,6621.4,//6607.9,6360.1
//			};
//	
//	double[] ftseAnnual = {
////			19.91,20.98,22.28,21.25,18.31,19.86,20.86,21.41,22.14,24.01,23.46,21.38,19.09,17.35,
////			18.48,17.58,16.23,19.13,20.19,20.13,19.11,19.98,21.86,23.88,45.53,77.76,35.79,29.41,28.43,25.97,26.37,
////			21.36,19.06,22.4,22.47,21.13,25.68,22.49,21.88,19.69,19.21,17.77,17.17,19.17,22.34,25.38,23.36,21.55,
////			18.79,17.37,16.98,17.05,18.58,18.82,17.03,16.92,17.17,17.68,17.1,17.08,18.62,19.2,22.39,25.99,26.54,
////			27.9,26.36,22.22,21.82,23.44,25.6,30.25,31.97,33,32.69,30.79,28.89,27.6,25.16,25.03,27.52,27.09,26.34,
////			24.82,23.15,23.03,23.08,22.9,23.39,26.93,26.8,25.44,25.03,25.36,26.88,30.2,36.47,38.75,39.09,39.6,40.25,
////			37.37,36.73,36.31,33.95,36.08,36.34,36.34,33.2,35.55,35.25,35.18,34.89,33.79,32.52,28.83,28.82,27.74,
////			27.95,28.37,29.16,24.62,28.13,29.94,30.54,31.89,32.85,35.35,38.36,39.8,35.59,28.69,23.29,30.3,31.75,34.23,
////			38.97,38.8,31.54,27.51,28.95,29.47,34.39,37.93,41.41,
////			42.32,48.3,49.36,47.13,45.72,42.19,47.19,40.17,
////			46.59,63.85,69.21,62.37,67.77,74.57,106.93,106.37,
////			118.47,102.51,107.86,
////			108.84,105.56,111.2,127.9,
////			173.72,179.58,153.03,
////			193.39,228.18,219.02,
////			150.53,160.63,172.64,
////			226.99,242.3,283.82,
////			313.07,338.64,389.24,
//			470.5,592.94,702.06,
//			835.48,1238.57,978.58,
//			1225.8,1226.83,1284.07,1363.97,1698.75,1754.48,
//			1802.57,2013.66,2492.41,2885.17,3242.06,3265.95,3045.55,2580
////AM: these are the checks..
//			,2207.4,2410.75,2847.02,
//			3236.11//,3490.17
//			};
//	int stepSize = 1;
//	int sampleLength =ftseAnnual.length-1;
//	int forecaststeps = 1;
//	
//	double[] assetPrices = ftseAnnual;
//	
//	double[][] totalResults = new double[(int)Math.ceil(((double)assetPrices.length-sampleLength)/(double)stepSize)][sampleLength+forecaststeps];  
//	loop=0;
//	
//	for(int i=0;i<assetPrices.length-sampleLength;){
//		double[] marketPrices=new double[sampleLength];
//		for(int j=0;j<sampleLength;j++){
//			marketPrices[j]=assetPrices[i+j];
//		}
//		double[] ds = matchAssetPrices(marketPrices);
//		totalResults[loop]=ds;
//		loop++;
//		i+=stepSize;
//	}
//	double[] returns = new double[assetPrices.length+forecaststeps];
//	double[][] filledin = new double[totalResults.length][assetPrices.length+forecaststeps];
//	for(int i=0;i<totalResults.length;i++){
//		double[] results = totalResults[i];
//		int spacesBefore = i*stepSize;
//		double[] toFill = filledin[i];
//		int j=0;
//		for(j=spacesBefore;j<spacesBefore+sampleLength+forecaststeps;j++){
//			if(j-spacesBefore>=results.length){
//				System.out.println("cock up in array management");
//			}else{
//				toFill[j]=results[j-spacesBefore];
//				if(j>spacesBefore+sampleLength-1){
//					returns[j]=(results[j-spacesBefore]-results[j-spacesBefore-1])/results[j-spacesBefore-1];
//				}
//			}
//		}
////		for(;j<ftseAnnual.length+5;j++){
////			//well no need, they're already zero.
////		}
//		
//	}
//	FileWriter writer = new FileWriter("C:\\temp\\final"+System.currentTimeMillis());
//    writer.append("\n\n ");
//	for(int j=0;j<filledin[0].length;j++){
//		for(int i=0;i<filledin.length;i++){
//			System.out.print("	"+filledin[i][j]);
//			writer.append("	"+filledin[i][j]);
//		}
//		System.out.println();
//		writer.append("\n");
//	}
//	writer.append("\n	returns");
//	writer.flush();
//	writer.append("\n	Returns	Asset real returns	asset price");
//	for(int i=sampleLength;i<returns.length-forecaststeps;i++){
//		writer.append("\n	"+returns[i]+"	"+(assetPrices[i]-assetPrices[i-1])/assetPrices[i-1]+"	"+assetPrices[i]);
//	}
//	
//	writer.flush();
//	writer.close();
//	//matchAssetPrices(ftseAnnual);
//  }
}
//
//
